//
//  CellForVideoSidList.m
//  世界
//
//  Created by admin on 16/2/14.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "CellForVideoSidList.h"
#import "CellForVideoType.h"
#import <UIImageView+WebCache.h>

@interface CellForVideoSidList ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;


@end

@implementation CellForVideoSidList

#pragma mark 重写 dealloc 方法
- (void)dealloc
{
    [_arrImgsrc release];
    
    [_arrTitle1 release];
    
    [_flowLayout release];
    
    [_collection release];
    
    [super dealloc];
}


#pragma mark 重写初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self != nil)
    {
        self.arrImgsrc = [NSMutableArray array];
        
        self.arrTitle1 = [NSMutableArray array];
        
        [self createCollectionView];
    }
    return self;
}


#pragma mark 布局子视图
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.collection.frame = self.contentView.frame;
    
    self.flowLayout.itemSize = CGSizeMake(self.contentView.frame.size.width/self.arrTitle1.count, self.contentView.frame.size.height);
}


#pragma mark 自定义方法 >>>>>>
#pragma mark 设置collectionView的布局方式
- (void)createFlowLayout
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //滑动方向
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //item之间的最小间距(纵向)
    self.flowLayout.minimumLineSpacing = 0;
}


#pragma mark 创建CollectionView
- (void)createCollectionView
{
    [self createFlowLayout];
    
    self.collection = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:self.flowLayout];
    
    [_flowLayout release];
    
    self.collection.backgroundColor = [UIColor whiteColor];
    
    [self.contentView addSubview: self.collection];
    
    //指定代理人
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    //注册Cell(collectionView 只支持先注册)
    [self.collection registerClass:[CellForVideoType class] forCellWithReuseIdentifier:@"pool1"];
}




#pragma mark UICollectionViewDataSource 协议方法 >>>>>>
#pragma mark 返回Item的个数 (required 1/2)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrTitle1.count;
}



#pragma mark 给每个Item上添加Cell (required 2/2)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CellForVideoType *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.arrImgsrc[indexPath.item]]];
    
    cell.lableForTitle.text = self.arrTitle1[indexPath.item];
    
    
    return cell;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [NSString stringWithFormat:@"%ld", indexPath.item];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Video1" object:nil userInfo:@{@"item" : str, @"title" : self.arrTitle1[indexPath.item]}];
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
